var configFlag:Boolean=false;
var configUri:String;

// var prevBlockFlag:Boolean;

function init(param:String, data, slot:String)
{
	var cont:String=Packages.system.MLF.scanDictionary(Packages.components.ReadingRecorder.recorderMain, "processResult05");
	Packages.components.ReadingRecorder.refreshResultContent(cont, true);
	var hlp:String=Packages.system.MLF.scanDictionary(Packages.components.ReadingRecorder.recorderMain, "recordHelpContent03");
	Packages.components.ReadingRecorder.refreshHelpContent(hlp);
	
	trace("component RecordingGuide init...");
	mlf.mcmanager.MessageAgent.getMessageDispatcher().addEventListener("sliderChange", this);
	var nc=mlf.mcmanager.MessageAgent.getMessageSlot("mlf", "defaultConnection");
	Packages.modula.MicrophoneDevice.initMicDevice(nc, "Conf");

	txtReaderInit();		// modula part later
	// TODO
	// add the audioId generator
	// request the audioId from the server, 
	// and save into the Packages.task.Composition.audioId
	generateAudioId();		// modula part later
	Packages.system.MLF.addListener("onColorPickerChanged", this);
}

function onColorPickerChanged(obj)
{
	// trace("onColorPickerChanged: "+obj.ins);
	var colorTf:TextField;
	var path=mlf.mcmanager.PathResolver.drawPath(obj.ins);
	if (path.indexOf("currentCp")>-1)
	{
		colorTf=mlf.gatekeeper.native.Caller.scanElement("RecordingGuide", "colorInput");
	}
	else if (path.indexOf("appendCp")>-1)
	{
		colorTf=mlf.gatekeeper.native.Caller.scanElement("RecordingGuide", "acolorInput");
	}
	// trace("colorTf: "+colorTf);
	colorTf.text="0x"+obj.param;
}

function generateAudioId()
{
	var item=new Object();
	item.type="generateUuid";
	item.param=new Object();
	item.result=new Object();
	item.result.onResult=function(res)
	{
		trace("generateAudioId.onResult: "+res.returnObj);
		if (res.returnObj!=null)
		{
			Packages.task.Composition.audioId=res.returnObj;
			configUri=Packages.task.Composition.audioId;
		}
	}	
	Packages.system.MLF.rtmpCaller(item);
}

function switchTab(param, data, slot)
{
	var tabArr:Array=["mictab", "formattab", "blocktab"];
	for (var a in tabArr)
	{
		var tabbg=mlf.gatekeeper.native.Caller.scanElement(slot, tabArr[a]);
		var tabpt=mlf.gatekeeper.native.Caller.scanElement(slot, tabArr[a]+"pt");
		tabbg._alpha=0;
		tabpt._visible=false;
	}
	var selectedTab=mlf.mcmanager.PathResolver.extractPath(param);
	var eleArr=param.split("-");
	var selectedTabPt=mlf.gatekeeper.native.Caller.scanElement(slot, eleArr[eleArr.length-1]+"pt");
	selectedTab._alpha=50;
	selectedTabPt._visible=true;
}

function txtReaderInit()
{
	var portlet=mlf.gatekeeper.native.Caller.scanElement("RecordingGuide", "txtReader");
	Packages.modula.TxtReader.getInstance(portlet);

	var docXML=new XML();
	docXML.ignoreWhite=true;
	docXML.onLoad=function()
	{
		Packages.task.Composition.content=this.toString();
		Packages.modula.TxtReader.display(Packages.task.Composition.content);
	}
	docXML.load(Packages.task.Composition.contentId);
}

function sliderChange(evtObj:Object)
{
	//trace("Message got, evtObj:"+evtObj);
	var target=mlf.mcmanager.PathResolver.drawPath(evtObj.target);
	//trace("target name:"+target+" index:"+target.indexOf("gslider"));
	if (target.toString().indexOf("gslider")>-1)
	{
		trace("gain slider pos:"+evtObj.param);
		Packages.modula.MicrophoneDevice.setGain(evtObj.param);
	}
	else if (target.toString().indexOf("rslider")>-1)
	{
		trace("rate slider pos:"+evtObj.param);
		var rate:Number;
		switch (Math.floor(evtObj.param/20)){
			case 0:
				rate=5;
				break;
			case 1:
				rate=8;
				break;
			case 2:
				rate=11;
				break;
			case 3:
				rate=22;
				break;
			case 4:
			case 5:
				rate=44;
				break;
		}
		Packages.modula.MicrophoneDevice.setRate(rate);
	}

	if (configFlag)
		Packages.modula.MicrophoneDevice.publish(configUri, "live");
}

function ladderChange(activityLevel:Number)
{
	//trace("ladderChange...activityLevel:"+activityLevel+" mode:"+mode);
	var ladder=mlf.gatekeeper.native.Caller.scanElement("RecordingGuide", "aladder");
	ladder.jnladder.pos=activityLevel;
	ladder.jnladder.draw();		
}

function startConf(param:String, data, slot:String)
{
	trace("start config...");
	configFlag=true;

	var cont:String=Packages.system.MLF.scanDictionary(Packages.components.ReadingRecorder.recorderMain, "processResult051");
	Packages.components.ReadingRecorder.refreshResultContent(cont, false);

	Packages.modula.MicrophoneDevice.publish(configUri, "live");
	Packages.modula.MicrophoneDevice.subscribe(configUri);
}

function confirmConf(param:String, data, slot:String)
{
	trace("confirm config...");
	configFlag=false;

	var cont:String=Packages.system.MLF.scanDictionary(Packages.components.ReadingRecorder.recorderMain, "processResult05");
	Packages.components.ReadingRecorder.refreshResultContent(cont, true);

	Packages.modula.MicrophoneDevice.saveMicSettings();
	Packages.modula.MicrophoneDevice.close();
}

function applyFormat(param:String, data:Object, slot:String)
{
	trace("applyFormat...");
	var fmtObj=new Object();
	if (data.size!="") fmtObj.size=Number(data.size);
	if (data.color!="") fmtObj.color=Number(data.color);
	if (data.lead!="") fmtObj.leading=Number(data.lead);
	var afmtObj=new Object();
	if (data.asize!="") afmtObj.size=Number(data.asize);
	if (data.acolor!="") afmtObj.color=Number(data.acolor);
	if (data.alead!="") afmtObj.leading=Number(data.alead);
	var fmtParam=new Object();
	fmtParam.currentTextFormat=fmtObj;
	fmtParam.appendTextFormat=afmtObj;
	Packages.task.Composition.readerSettings.fmt=fmtParam;
	Packages.modula.TxtReader.applyFormat(fmtParam);
}

function setBlock(param:String, data:Object, slot:String)
{
	var blockParam=new Object();
	blockParam.size=data.size;
	blockParam.speed=data.speed;
	Packages.task.Composition.readerSettings.block=blockParam;
	Packages.modula.TxtReader.setBlock(blockParam, true);
}

function nextBlock()
{
	Packages.modula.TxtReader.switchBlock(1);
}

function prevBlock()
{
	Packages.modula.TxtReader.switchBlock(-1);
}
/*
function enterRecordingSpace(param, data, slot)
{
	Packages.modula.TxtReader.removeInstance();
	Packages.components.Scene.enterRecordingSpace(param, data, slot);
}
*/
function switchHighlightBg(param:String):String
{
	var name:String;
	if (param.indexOf("mictabpt")>-1)
	{
		name="michighlightBg";
	}
	else if (param.indexOf("formattabpt")>-1)
	{
		name="fmthighlightBg";
	}
	else if (param.indexOf("blocktabpt")>-1)
	{
		name="blkhighlightBg";
	}

	return name;
}

function rolloutCon(param, data, slot)
{
	var highlightName=switchHighlightBg(param);
	mlf.mcmanager.PathResolver.extractPath(param+"Bg")._visible=true;
	Packages.system.Platform.highlightBG(mlf.mcmanager.PathResolver.extractPath(param), mlf.gatekeeper.native.Caller.scanElement(slot, highlightName), 0);
}

function rolloverCon(param, data, slot)
{
	var highlightName=switchHighlightBg(param);
	mlf.mcmanager.PathResolver.extractPath(param+"Bg")._visible=false;
	Packages.system.Platform.highlightBG(mlf.mcmanager.PathResolver.extractPath(param), mlf.gatekeeper.native.Caller.scanElement(slot, highlightName), 50);
}

function pressCon(param, data, slot)
{
	var highlightName=switchHighlightBg(param);
	Packages.system.Platform.highlightBG(mlf.mcmanager.PathResolver.extractPath(param), mlf.gatekeeper.native.Caller.scanElement(slot, highlightName), 100);
}

function releaseCon(param, data, slot)
{
	rolloverCon(param, data, slot);
	if (param.indexOf("start")>-1) startConf(param, data, slot);
	else if (param.indexOf("confirm")>-1) confirmConf(param, data, slot);
	else if (param.indexOf("applyformat")>-1) applyFormat(param, data, slot);
	else if (param.indexOf("nextblock")>-1) nextBlock(param, data, slot);
	else if (param.indexOf("prevblock")>-1) prevBlock(param, data, slot);
	else if (param.indexOf("setblock")>-1) setBlock(param, data, slot);
}